package com.useeinfo.oa.modules.maintaining.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.maintaining.entity.PlannerOrderLog;
import com.useeinfo.oa.modules.maintaining.enums.OrderOperationEnum;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: xudong.sun
 * @Date: 2019/1/3 10:33
 * @Description:
 */
@Repository("plannerOrderLogDao")
public class PlannerOrderLogDao implements CrudDao<PlannerOrderLog> {

    private final static Logger logger = LoggerFactory.getLogger(PlannerOrderLogDao.class);

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();
        conditionHash.put("s.deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        String enterpriseId = queryHash.get("enterpriseId");
        if (!StringUtils.isEmpty(enterpriseId)) {
            conditionHash.put("(s.enterprise.id = ?{paramIndex}) ", StringConverters.ToLong(enterpriseId));
        }
        String searchCode = queryHash.get("searchCode");
        if (!StringUtils.isEmpty(searchCode)) {
            conditionHash.put("(s.userName like ?{paramIndex} or s.detail like ?{paramIndex}) ","%"+searchCode+"%");
        }
        String name = queryHash.get("name");
        if (!StringUtils.isEmpty(name)) {
            conditionHash.put("(s.plannerOrder.name like ?{paramIndex}) ", "%"+name+"%");
        }
        String detail = queryHash.get("detail");
        if (!StringUtils.isEmpty(detail)) {
            conditionHash.put("(s.detail like ?{paramIndex}) ","%"+detail+"%" );
        }
        String startTime = queryHash.get("startTime");
        if (!StringUtils.isEmpty(startTime)) {
            conditionHash.put("(s.createDate >= ?{paramIndex}) ", DateTimeUtils.parse("yyyy-MM-dd HH:mm",startTime));
        }
        String endTime = queryHash.get("endTime");
        if (!StringUtils.isEmpty(endTime)) {
            conditionHash.put("(s.createDate <= ?{paramIndex}) ", DateTimeUtils.parse("yyyy-MM-dd HH:mm",endTime));
        }
        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(s) from PlannerOrderLog s ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<PlannerOrderLog> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select s from PlannerOrderLog s ", "order by s.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<PlannerOrderLog> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, PlannerOrderLog.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public PlannerOrderLog findModel(Long id) {
        return entityManager.find(PlannerOrderLog.class, id);
    }

    @Override
    public Integer add(PlannerOrderLog model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("PlannerOrderLogDaoImpl添加plannerOrderLog成功！");
        return 1;
    }

    @Override
    public Integer update(PlannerOrderLog model) {
        model.setUpdateDate(new Date());
        entityManager.merge(model);
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        PlannerOrderLog plannerOrderLog = entityManager.find(PlannerOrderLog.class, id);
        entityManager.remove(plannerOrderLog);
        return 1;
    }

    @Override
    public Integer deleteLogic(PlannerOrderLog model) {
        model.setDeleteStatus(true);
        return 1;
    }
}
